<template>
  <div class="receipt-container" ref="receiptRef">
    <div class="receipt">
      <!-- 店铺信息 -->
      <div class="shop-info">
        <h1 class="shop-name">{{ shopInfo.name || '啤酒屋POS' }}</h1>
        <p class="shop-address">{{ shopInfo.address || '地址：北京市朝阳区xxx街道xxx号' }}</p>
        <p class="shop-phone">电话：{{ shopInfo.phone || '010-12345678' }}</p>
      </div>

      <!-- 分割线 -->
      <div class="divider">================================</div>

      <!-- 订单信息 -->
      <div class="order-info">
        <div class="info-row">
          <span class="label">订单号：</span>
          <span class="value">{{ order.orderNo }}</span>
        </div>
        <div class="info-row">
          <span class="label">时间：</span>
          <span class="value">{{ formatDateTime(order.createdAt) }}</span>
        </div>
        <div class="info-row">
          <span class="label">收银员：</span>
          <span class="value">{{ order.user ? order.user.name : '未知' }}</span>
        </div>
        <div class="info-row">
          <span class="label">渠道：</span>
          <span class="value">{{ order.channel ? order.channel.name : '店内' }}</span>
        </div>
      </div>

      <!-- 分割线 -->
      <div class="divider">================================</div>

      <!-- 商品列表 -->
      <div class="items-section">
        <div class="items-header">
          <span class="item-name">商品</span>
          <span class="item-qty">数量</span>
          <span class="item-price">单价</span>
          <span class="item-total">小计</span>
        </div>
        <div class="items-divider">--------------------------------</div>
        
        <div v-for="item in order.items" :key="item.id" class="item-row">
          <div class="item-name-full">{{ item.product.name }}</div>
          <div class="item-details">
            <span class="item-qty">{{ item.quantity }}</span>
            <span class="item-price">¥{{ item.price.toFixed(2) }}</span>
            <span class="item-total">¥{{ (item.price * item.quantity).toFixed(2) }}</span>
          </div>
        </div>
      </div>

      <!-- 分割线 -->
      <div class="divider">================================</div>

      <!-- 合计信息 -->
      <div class="total-section">
        <div class="total-row">
          <span class="label">商品数量：</span>
          <span class="value">{{ getTotalQuantity() }} 件</span>
        </div>
        <div class="total-row">
          <span class="label">优惠金额：</span>
          <span class="value">¥0.00</span>
        </div>
        <div class="total-row total-amount">
          <span class="label">合计金额：</span>
          <span class="value">¥{{ order.total.toFixed(2) }}</span>
        </div>
      </div>

      <!-- 支付信息 -->
      <div v-if="order.payments && order.payments.length > 0" class="payment-section">
        <div class="divider">--------------------------------</div>
        <div v-for="payment in order.payments" :key="payment.id" class="payment-row">
          <span class="label">{{ getPaymentMethodText(payment.method) }}：</span>
          <span class="value">¥{{ payment.amount.toFixed(2) }}</span>
        </div>
      </div>

      <!-- 备注信息 -->
      <div v-if="order.note" class="note-section">
        <div class="divider">--------------------------------</div>
        <div class="note-label">备注：</div>
        <div class="note-content">{{ order.note }}</div>
      </div>

      <!-- 分割线 -->
      <div class="divider">================================</div>

      <!-- 底部信息 -->
      <div class="footer-info">
        <p class="thank-you">谢谢惠顾，欢迎再次光临！</p>
        <p class="print-time">打印时间：{{ getCurrentTime() }}</p>
        <p class="system-info">系统：啤酒屋POS v1.0</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'

const props = defineProps({
  order: {
    type: Object,
    required: true
  },
  shopInfo: {
    type: Object,
    default: () => ({
      name: '啤酒屋POS',
      address: '地址：北京市朝阳区xxx街道xxx号',
      phone: '010-12345678'
    })
  }
})

const receiptRef = ref(null)

// 格式化日期时间
const formatDateTime = (dateString) => {
  const date = new Date(dateString)
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')} ${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}:${String(date.getSeconds()).padStart(2, '0')}`
}

// 获取当前时间
const getCurrentTime = () => {
  return formatDateTime(new Date())
}

// 计算总数量
const getTotalQuantity = () => {
  return props.order.items.reduce((total, item) => total + item.quantity, 0)
}

// 获取支付方式文本
const getPaymentMethodText = (method) => {
  const methodMap = {
    'cash': '现金',
    'wechat': '微信支付',
    'alipay': '支付宝',
    'mixed': '混合支付'
  }
  return methodMap[method] || method
}

// 打印功能
const print = () => {
  const printContent = receiptRef.value.innerHTML
  const printWindow = window.open('', '_blank')
  printWindow.document.write(`
    <html>
      <head>
        <title>订单小票 - ${props.order.orderNo}</title>
        <style>
          body { margin: 0; padding: 10px; font-family: 'Courier New', monospace; }
          .receipt { width: 58mm; margin: 0 auto; }
          @media print {
            body { margin: 0; padding: 0; }
            .receipt { width: 100%; }
          }
        </style>
      </head>
      <body>
        ${printContent}
      </body>
    </html>
  `)
  printWindow.document.close()
  printWindow.print()
  printWindow.close()
}

// 暴露打印方法
defineExpose({
  print
})
</script>

<style scoped>
.receipt-container {
  display: flex;
  justify-content: center;
  padding: 20px;
  background: #f5f5f5;
}

.receipt {
  width: 58mm;
  background: white;
  padding: 8px;
  font-family: 'Courier New', monospace;
  font-size: 12px;
  line-height: 1.2;
  color: #000;
  border: 1px solid #ddd;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

/* 店铺信息 */
.shop-info {
  text-align: center;
  margin-bottom: 8px;
}

.shop-name {
  font-size: 16px;
  font-weight: bold;
  margin: 0 0 4px 0;
}

.shop-address,
.shop-phone {
  font-size: 10px;
  margin: 2px 0;
}

/* 分割线 */
.divider {
  text-align: center;
  font-size: 10px;
  margin: 4px 0;
  overflow: hidden;
}

/* 订单信息 */
.order-info {
  margin: 8px 0;
}

.info-row {
  display: flex;
  justify-content: space-between;
  margin: 2px 0;
  font-size: 10px;
}

.label {
  font-weight: bold;
}

/* 商品列表 */
.items-section {
  margin: 8px 0;
}

.items-header {
  display: flex;
  font-weight: bold;
  font-size: 10px;
  margin-bottom: 2px;
}

.items-header .item-name {
  flex: 2;
}

.items-header .item-qty,
.items-header .item-price,
.items-header .item-total {
  flex: 1;
  text-align: right;
}

.items-divider {
  font-size: 10px;
  margin: 2px 0;
}

.item-row {
  margin: 4px 0;
}

.item-name-full {
  font-size: 10px;
  font-weight: bold;
  margin-bottom: 1px;
}

.item-details {
  display: flex;
  font-size: 10px;
}

.item-details .item-qty {
  flex: 1;
  text-align: left;
}

.item-details .item-price,
.item-details .item-total {
  flex: 1;
  text-align: right;
}

/* 合计信息 */
.total-section {
  margin: 8px 0;
}

.total-row {
  display: flex;
  justify-content: space-between;
  margin: 2px 0;
  font-size: 10px;
}

.total-amount {
  font-weight: bold;
  font-size: 12px;
  border-top: 1px solid #000;
  padding-top: 2px;
  margin-top: 4px;
}

/* 支付信息 */
.payment-section {
  margin: 8px 0;
}

.payment-row {
  display: flex;
  justify-content: space-between;
  margin: 2px 0;
  font-size: 10px;
}

/* 备注信息 */
.note-section {
  margin: 8px 0;
}

.note-label {
  font-weight: bold;
  font-size: 10px;
  margin-bottom: 2px;
}

.note-content {
  font-size: 10px;
  word-wrap: break-word;
}

/* 底部信息 */
.footer-info {
  text-align: center;
  margin-top: 8px;
}

.thank-you {
  font-weight: bold;
  font-size: 11px;
  margin: 4px 0;
}

.print-time,
.system-info {
  font-size: 9px;
  margin: 2px 0;
  color: #666;
}

/* 打印样式 */
@media print {
  .receipt-container {
    padding: 0;
    background: none;
  }
  
  .receipt {
    width: 100%;
    border: none;
    box-shadow: none;
    padding: 0;
  }
}
</style>
